/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converters;
import be.TipoDocente;
import beans.ManagedBeanTipoDocente;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
/**
 *
 * Sistema de Control Acádemico (saca) creado por Buñay Paulo
 */

@FacesConverter(value = "tipodocenteConverter")
public class TipoDocenteConverter implements Converter{
    
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (context == null) {
throw new NullPointerException("context");
}
if (component == null) {
throw new NullPointerException("component");
}
      FacesContext ctx = FacesContext.getCurrentInstance();
      ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(),"#{managedBeanTipoDocente}",ManagedBeanTipoDocente.class);
ManagedBeanTipoDocente tipodocentes = (ManagedBeanTipoDocente)vex.getValue(ctx.getELContext());
TipoDocente  tipodocente;
try {
  tipodocente =  tipodocentes.getTipoDocente(new Integer(value));
} catch( NumberFormatException e ) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Valor Desconocido", "Este no es un Tipo Docente" );
throw new ConverterException( message );
}
if( tipodocente == null ) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Valor Desconocido", "El Tipo Docente desconocido" );
throw new ConverterException( message );
}
return tipodocente;
}



    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (context == null){throw new NullPointerException("context");}
if (component == null){throw new NullPointerException("component");}
return ((TipoDocente)value).getIdTipoDocente().toString();
    }

}